import fetcher from "../apis/Fetcher";
import {NumberIdAndName} from "../types/BasicModels";
import {QueryGoodsResponse} from "../apis/GoodsApi";
import {Page, Pageable} from "../utils/Page";
import {OffsetDateTime} from "@js-joda/core";

export function createCheckInventoryRecord(id: number) {
    return fetcher.post<number>(`checkInventoryRecords/${id}/orders`)
}

export function getCheckInventoryRecordDetail(id: number) {
    return fetcher.get<CheckInventoryRecordDetail>(`checkInventoryRecords/${id}`, {
        includeItems: true
    })
}

export function getCheckInventoryRecordDetailWithoutItems(id: number) {
    return fetcher.get<CheckInventoryRecordDetail>(`checkInventoryRecords/${id}`)
}

export interface CheckInventoryRecordDetail {
    id: number
    name:string
    createdDateTime: OffsetDateTime
    createdBy: NumberIdAndName
    items: Array<CheckInventoryRecordItem>
    submitDateTime?: OffsetDateTime
}

export interface CheckInventoryRecordItem extends QueryGoodsResponse {
    amountInOrder: number
}

export interface AddCheckInventoryRecordItemRequest {
    amount: number
    goodsId: string
}

export function addCheckInventoryRecordItem(id: number, addCheckInventoryRecordItemRequest: AddCheckInventoryRecordItemRequest) {
    return fetcher.post(`checkInventoryRecords/${id}/items`, {}, addCheckInventoryRecordItemRequest)
}

export function deleteCheckInventoryRecordItem(orderId: number, goodsId: number) {
    return fetcher.delete(`checkInventoryRecords/${orderId}/items/${goodsId}`)
}

export interface UpdateCheckInventoryRecordItemAmountRequest {
    amount: number
}

export function updateCheckInventoryRecordItemAmount(orderId: number, goodsId: number, updateCheckInventoryRecordItemAmountRequest: UpdateCheckInventoryRecordItemAmountRequest) {
    return fetcher.patch(`checkInventoryRecords/${orderId}/items/${goodsId}/amount`, {}, updateCheckInventoryRecordItemAmountRequest)
}

export interface QueryCheckInventoryRecordRequest {
    startDate?: string
    endDate?: string
    employeeIds: number[]
    submitted?:boolean
}

export function queryCheckInventoryRecords(queryCheckInventoryRecordRequest: QueryCheckInventoryRecordRequest, pageable: Pageable) {
    return fetcher.get<Page<QueryCheckInventoryRecordResponse>>("checkInventoryRecords", {...queryCheckInventoryRecordRequest, ...pageable})
}

export interface QueryCheckInventoryRecordResponse {
    id: number
    createdBy: NumberIdAndName
    createdDateTime: OffsetDateTime
    goodsKindAmount: number
    name:string
    submittedDateTime?: OffsetDateTime
}

export function getAllCheckInventoryRecordCreatedEmployees() {
    return fetcher.get<Array<NumberIdAndName>>("checkInventoryRecords/employees", {projection: "summary"})
}

export function submitCheckInventoryRecord(id: number) {
    return fetcher.patch<number>(`checkInventoryRecords/${id}/submit`)
}

export function convertToDeliveryOrder(id: number) {
    return fetcher.post<number>(`checkInventoryRecords/${id}/delivery`)
}